package webdev.fragment;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import webdev.customlib.zoomphoto.adapter.AppConstant;
import webdev.customlib.zoomphoto.adapter.GridViewImageAdapter;
import webdev.customlib.zoomphoto.adapter.Utils;
import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.GridView;
import dev.danang.R;

public class PhotosFragment extends BaseFragment implements
		OnClickListener {

	private Utils utils;
	private ArrayList<String> imagePaths = new ArrayList<String>();
	private GridViewImageAdapter adapter;
	private GridView gridView;
	private int columnWidth;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View layout = inflater.inflate(R.layout.activity_grid_view, null);
		init(layout);
		setValueForMembers();
		setEventForMembers();


		return layout;
	}

	
	@Override
	protected void init(View layout) {
		// TODO Auto-generated method stub
		gridView = (GridView) layout.findViewById(R.id.grid_view);

		utils = new Utils(getActivity());

		// Initilizing Grid View
		InitilizeGridLayout();

		// loading all image paths from SD card
		try {
			List<String> arr = getImage(getActivity());
			for (int i = 0; i < arr.size(); i++) {
				imagePaths.add(arr.get(i));
			}
//			imagePaths = getImage(getActivity());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}//utils.getFilePaths();

		// Gridview adapter
		adapter = new GridViewImageAdapter(getActivity(), imagePaths,
				columnWidth);

		// setting grid view adapter
		gridView.setAdapter(adapter);
	}
	private List<String> getImage(Context conetx) throws IOException
    {
      AssetManager assetManager =conetx.getAssets();
      String[] files = assetManager.list("image");   
      List<String> it =  Arrays.asList(files);
      return it; 

      }
	private void InitilizeGridLayout() {
		Resources r = getResources();
		float padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
				AppConstant.GRID_PADDING, r.getDisplayMetrics());

		columnWidth = (int) ((utils.getScreenWidth() - ((AppConstant.NUM_OF_COLUMNS + 1) * padding)) / AppConstant.NUM_OF_COLUMNS);

		gridView.setNumColumns(AppConstant.NUM_OF_COLUMNS);
		gridView.setColumnWidth(columnWidth);
		gridView.setStretchMode(GridView.NO_STRETCH);
		gridView.setPadding((int) padding, (int) padding, (int) padding,
				(int) padding);
		gridView.setHorizontalSpacing((int) padding);
		gridView.setVerticalSpacing((int) padding);
	}
	
	@Override
	protected void setValueForMembers() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void setEventForMembers() {
		// TODO Auto-generated method stub
	}

	
	@Override
	protected String getUrl() {
		// TODO Auto-generated method stub
		return null;
	}

	
	@Override
	public void onClick(View v) {

	}

}
